package com.doraemon.controller.admin;

import com.doraemon.dto.OrdersCancelDTO;
import com.doraemon.dto.OrdersConfirmDTO;
import com.doraemon.dto.OrdersPageQueryDTO;
import com.doraemon.dto.OrdersRejectionDTO;
import com.doraemon.result.PageResult;
import com.doraemon.result.Result;
import com.doraemon.service.OrderService;
import com.doraemon.vo.OrderStatisticsVO;
import com.doraemon.vo.OrderVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 订单管理Controller
 **/
@RestController
@RequestMapping("/admin/order")
@Slf4j
@Api(tags = "订单相关接口")
public class OrderController {

    @Autowired
    private OrderService orderService;

    /***
     * 订单搜索
     * ---
     * @param ordersPageQueryDTO 订单分页DTO类
     * @return: com.doraemon.result.Result<com.doraemon.result.PageResult>
     */
    @GetMapping("/conditionSearch")
    @ApiOperation("订单搜索")
    public Result<PageResult> conditionSearch(OrdersPageQueryDTO ordersPageQueryDTO) { // 对于Get请求参数要封装成实体接收，不需要用@RequestBody注解
        log.info("订单搜索：{}", ordersPageQueryDTO);

        // 调用Service层 conditionSearch 方法，获取查询到的分页结果集对象pageResult
        PageResult pageResult = orderService.conditionSearch(ordersPageQueryDTO);

        return Result.success(pageResult);
    }

    /***
     * 各个状态的订单数量统计
     * ---
     * @param
     * @return: com.doraemon.result.Result<com.doraemon.vo.OrderStatisticsVO>
     */
    @GetMapping("/statistics")
    @ApiOperation("各个状态的订单数量统计")
    public Result<OrderStatisticsVO> statistics() {

        // 调用Service层 statistics 方法，获取查询到的orderStatisticsVO类
        OrderStatisticsVO orderStatisticsVO = orderService.statistics();

        return Result.success(orderStatisticsVO);
    }

    /***
     * 查询订单详情
     * ---
     * @param orderId 订单id
     * @return: com.doraemon.result.Result<com.doraemon.vo.OrderVO>
     */
    @GetMapping("/details/{id}")
    @ApiOperation("查询订单详情")
    public Result<OrderVO> details(@PathVariable("id") Long orderId) {
        log.info("查询订单详情：{}", orderId);

        // 调用Service层 details 方法，获取查询到的订单VO类
        OrderVO orderVO = orderService.details(orderId);

        return Result.success(orderVO);
    }

    /***
     * 接单
     * ---
     * @param ordersConfirmDTO 订单接单DTO类
     * @return: com.doraemon.result.Result
     */
    @PutMapping("/confirm")
    @ApiOperation("接单")
    public Result confirm(@RequestBody OrdersConfirmDTO ordersConfirmDTO) {
        log.info("接单：{}", ordersConfirmDTO);

        // 调用Service层 confirm 方法，对指定订单进行接单处理
        orderService.confirm(ordersConfirmDTO);

        return Result.success();
    }

    /***
     * 拒单
     * ---
     * @param ordersRejectionDTO 订单拒单DTO类
     * @return: com.doraemon.result.Result
     */
    @PutMapping("/rejection")
    @ApiOperation("拒单")
    public Result rejection(@RequestBody OrdersRejectionDTO ordersRejectionDTO) {
        log.info("拒单：{}", ordersRejectionDTO);

        // 调用Service层 rejection 方法，对指定订单进行拒单处理
        orderService.rejection(ordersRejectionDTO);

        return Result.success();
    }

    /***
     * 取消订单
     * ---
     * @param ordersCancelDTO 订单取消DTO类
     * @return: com.doraemon.result.Result
     */
    @PutMapping("/cancel")
    @ApiOperation("取消订单")
    public Result cancel(@RequestBody OrdersCancelDTO ordersCancelDTO) {
        log.info("取消订单：{}", ordersCancelDTO);

        // 调用Service层 cancel 方法，对指定订单进行取消处理
        orderService.cancel(ordersCancelDTO);

        return Result.success();
    }

    /***
     * 派送订单
     * ---
     * @param orderId 订单id
     * @return: com.doraemon.result.Result
     */
    @PutMapping("/delivery/{id}")
    @ApiOperation("派送订单")
    public Result delivery(@PathVariable("id") Long orderId) {
        log.info("派送订单：{}", orderId);

        // 调用Service层 delivery 方法，对指定订单进行派送处理
        orderService.delivery(orderId);

        return Result.success();
    }

    /***
     * 完成订单
     * ---
     * @param orderId 订单id
     * @return: com.doraemon.result.Result
     */
    @PutMapping("/complete/{id}")
    @ApiOperation("完成订单")
    public Result complete(@PathVariable("id") Long orderId) {
        log.info("完成订单：{}", orderId);

        // 调用Service层 complete 方法，对指定订单进行完成处理
        orderService.complete(orderId);

        return Result.success();
    }

}
